﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;

namespace Elentok.Common.Wpf.Demos.ViewModels
{
  public sealed class ShellViewModel
  {
    public ObservableCollection<IDemoViewModel> Demos { get; private set; }

    public ShellViewModel()
    {
      this.Demos = new ObservableCollection<IDemoViewModel>();
      LoadAssembly(Assembly.GetExecutingAssembly());
    }

    private void LoadAssembly(Assembly assembly)
    {
      var demoViewModelType = typeof(IDemoViewModel);

      foreach (Module module in assembly.GetModules())
      {
        foreach (Type type in module.GetTypes())
        {
          if (!type.IsAbstract && demoViewModelType.IsAssignableFrom(type))
          {
            var viewModel = (IDemoViewModel)Activator.CreateInstance(type);
            this.Demos.Add(viewModel);
          }
        }
      }
    }
  }
}
